package com.lgt_enterprise_android.view.pages;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lgt_enterprise_android.BuildConfig;
import com.lgt_enterprise_android.Constant;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.CommonUtil;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.KeyboardStateObserver;
import com.lgt_enterprise_android.utils.PrefUtils;
import com.lgt_enterprise_android.utils.StringUtil;
import com.lgt_enterprise_android.view.MainActivity;
import com.lgt_enterprise_android.view.pages.me.EditInfoActivity;

import java.lang.ref.WeakReference;

public class LoginActivity extends BaseActivity {

    private TextView txtGetMsgCode;
    private View btnLogin;
    private EditText edPhone, edMsgCode;
    private TextView txtTip;

    private View viewScroll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        statusBarColor = R.color.white;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        bindEvent();
    }

    protected void initView() {
        viewScroll = findViewById(R.id.lin_scroll);
        txtGetMsgCode = (TextView) findViewById(R.id.txt_get_msgcode);
        btnLogin = findViewById(R.id.btn_login);
        btnLogin.setEnabled(false);
        edPhone = (EditText) findViewById(R.id.ed_phone_no);
        txtTip = (TextView) findViewById(R.id.txt_tip);
        edMsgCode = (EditText) findViewById(R.id.ed_msg_code);

    }


    protected void bindEvent() {
        KeyboardStateObserver.getKeyboardStateObserver(this).setKeyboardVisibilityListener(new KeyboardStateObserver.OnKeyboardVisibilityListener() {
            @Override
            public void onKeyboardShow(int height) {
                CommonUtil.scrollToPos(0, height - dp2Px(150), viewScroll);
            }

            @Override
            public void onKeyboardHide(int height) {
                CommonUtil.scrollToPos(dp2Px(60), 0, viewScroll);
            }
        });

        findViewById(R.id.txt_change_pass).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, Login2Activity.class);
                startActivity(intent);
                finish();
            }
        });

        findViewById(R.id.txt_ip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(BuildConfig.flavor_Type==2){
                    Intent intent = new Intent(mContext, EditInfoActivity.class);
                    intent.putExtra("type", EditInfoActivity.IP);
                    startActivity(intent);
                }
            }
        });

        edPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                txtTip.setText("");
                if (edPhone.getText().toString().length() == 11 && edMsgCode.getText().toString().length() == 6) {
                    btnLogin.setEnabled(true);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button));
                } else {
                    btnLogin.setEnabled(false);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button_disable));
                }
            }
        });

        edMsgCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                txtTip.setText("");
                if (edPhone.getText().toString().length() == 11 && edMsgCode.getText().toString().length() == 6) {
                    btnLogin.setEnabled(true);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button));
                } else {
                    btnLogin.setEnabled(false);
                    btnLogin.setBackground(getDrawable(R.drawable.bg_button_disable));
                }
            }
        });


        findViewById(R.id.txt_protocl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mContext,WebViewActivity.class);
                intent.putExtra("url",Constant.PRIVACY_AGREEMENT);
                startActivity(intent);
            }
        });

        txtGetMsgCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String strPhone = edPhone.getText().toString().trim();
                if (StringUtil.isEmpty(strPhone)) {
                    txtTip.setText("手机号不能为空");
                    return;
                }

                if (!CommonUtil.isPhoneNumValid(strPhone)) {
                    txtTip.setText("手机号格式不正确");
                    return;
                }

                HttpManager.sendmsgCode(mContext, strPhone, "LOGIN_ENTERPRISE", new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        showToast("验证码发送成功");
                        edMsgCode.requestFocus();
                        CommonUtil.startTimer(new WeakReference<>(txtGetMsgCode), "获取验证码", 60, 1);
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });
            }
        });

        findViewById(R.id.btn_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CheckBox ch=(CheckBox)findViewById(R.id.ch);
                if(!ch.isChecked()){
                    showToast("请同意用户协议及隐私政策");
                    return;
                }


                String strPhone = edPhone.getText().toString().trim();
                if (StringUtil.isEmpty(strPhone)) {
                    txtTip.setText("手机号不能为空");
                    return;
                }

                if (!CommonUtil.isPhoneNumValid(strPhone)) {
                    txtTip.setText("手机号格式不正确");
                    return;
                }

                String msgcode = edMsgCode.getText().toString().trim();

                showLoadingDialog();
                HttpManager.smsLogin(mContext, strPhone,msgcode, new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {

                        if (resonseObj instanceof JSONObject) {
                            JSONObject jo = (JSONObject) resonseObj;
                            Constant.token = jo.getString("token");

                            dismissLoadingDialog();
                            PrefUtils.setSharedPrefStringData(mContext, PrefUtils.TOKEN, Constant.token);

                            Intent intent = new Intent(mContext, MainActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(intent);
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(String mes) {
                        dismissLoadingDialog();
                        txtTip.setText(mes);
                    }
                });
            }
        });
    }
}
